package com.dcivision.form.utility;
/**
 * Common utility functions which used in form test case.
 * 
 * @author Bill
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;

import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;

import com.dcivision.form.bean.FormGroup;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TLog;
import com.dcivision.user.bean.UserRecordPermission;
import com.dcivision.user.dao.UserRecordPermissionDAObject;
import com.dcivision.workflow.utility.WFObjectFactory;


public class FormUtility {
  private static IDataSet deleteAllDataset;	
  
  /**
   * Set permission for form group by form group id and user id.
   * 
   * @param  group
   * @param  userRecord
   * @return boolean
   * @throws Exception
   * @author Bill
   */
  public static boolean setFormGroupPermission(FormGroup group,Integer userRecordID,SessionContainer sessionCtn,Connection connection) throws Exception{
    //set form group permission
	String permission="RIECDMGF";  //All permission
	UserRecordPermission userPerm = new UserRecordPermission();
	userPerm.setObjectType(GlobalConstant.OBJECT_TYPE_FORM);
	userPerm.setObjectID(group.getID());
	userPerm.setUserRecordID(userRecordID);
	userPerm.setPermission(permission);
	UserRecordPermissionDAObject userPermDAO = new UserRecordPermissionDAObject(sessionCtn, connection);
	try{
	  userPermDAO.insertObject(userPerm);
	  return true;
	}catch(Exception ex){
	  TLog.log.info(ex.getMessage());
	  throw ex;
	}
  }
  
  /**
   * Get a delete_all tables for form test
   * 
   * @return IDataSet
   * @throws IOException
   * @throws DataSetException
   * @throws FileNotFoundException
   * @author Bill
   */
  public static IDataSet getDeleteAllDataSet() throws IOException, DataSetException, FileNotFoundException {
    if (deleteAllDataset != null) {
      return deleteAllDataset;
    }
    deleteAllDataset = new FlatXmlDataSet(WFObjectFactory.class.getResourceAsStream("/dbunit/eip318StandardDeleteAll.xml"));
    return deleteAllDataset;
  }
}
